package com.vhall.component.service.goods.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vhall.component.dao.goods.GoodsMapper;
import com.vhall.component.dao.goods.LiveGoodsMapper;
import com.vhall.component.dao.goods.LiveGoodsStatsMapper;
import com.vhall.component.entity.goods.GoodsEntity;
import com.vhall.component.entity.goods.goods.GoodsPage;
import com.vhall.component.entity.goods.goods.LiveGoodsStatsConsoleReqVO;
import com.vhall.component.entity.goods.goods.LiveGoodsStatsConsoleRspVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.framework.common.utils.CollectionUtils;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.goods.LiveGoodsStatsConsoleService;
import com.vhall.component.service.goods.common.GoodsConstant;
import com.vhall.component.service.room.RoomInnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author guoxin.wang
 * @date 2021-06-10 15:21
 */
@Slf4j
@Service
public class LiveGoodsStatsConsoleServiceImpl implements LiveGoodsStatsConsoleService {

    @Autowired
    private RoomInnerService roomInnerService;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private LiveGoodsMapper liveGoodsMapper;

    @Autowired
    private LiveGoodsStatsMapper liveGoodsStatsMapper;

    @Override
    public GoodsPage<LiveGoodsStatsConsoleRspVO> listStats(LiveGoodsStatsConsoleReqVO reqVO) {
        reqVO.setDefault();
        // 2、获取房间信息。
        RoomsEntityDTO roomInfo = roomInnerService.getRoomInfo(reqVO.getRoomId());
        if (roomInfo == null) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        // 3、分页查询商品信息。
        Page<LiveGoodsStatsConsoleRspVO> pageLiveGoods = new Page<>(reqVO.getPage(), reqVO.getPageSize());

        Map<String, Object> param = new HashMap<>(4);
        param.put("ilId", roomInfo.getIlId());
        param.put("goodsStatus", GoodsConstant.GOODS_STATUS_ON);
        if (StringUtils.isNotBlank(reqVO.getBeginTime())) {
            param.put("beginTime", reqVO.getBeginTime());
        }
        if (StringUtils.isNotBlank(reqVO.getEndTime())) {
            param.put("endTime", reqVO.getEndTime());
        }
        Page<LiveGoodsStatsConsoleRspVO> liveGoods = liveGoodsMapper.getStatList(pageLiveGoods, param);
        List<LiveGoodsStatsConsoleRspVO> records = liveGoods.getRecords();
        long total = liveGoods.getTotal();
        if (CollectionUtils.isEmpty(records)) {
            return new GoodsPage<>(reqVO.getPage(), reqVO.getPageSize(), (int) total, new ArrayList<>(), GoodsConstant.LIVE_GOODS_ENTRANCE_SWITCH_CLOSE);
        }
        // 4、搜集所有的商品id。
        List<Integer> bgIds = records.stream().map(LiveGoodsStatsConsoleRspVO::getBgId).collect(Collectors.toList());
        // 6、获取商品信息。
        LambdaQueryWrapper<GoodsEntity> queryGoods = Wrappers.lambdaQuery();
        queryGoods.in(GoodsEntity::getBgId, bgIds);
        queryGoods.eq(GoodsEntity::getGoodsStatus, GoodsConstant.GOODS_STATUS_ON);
        List<GoodsEntity> goods = goodsMapper.selectList(queryGoods);
        Map<Integer, GoodsEntity> goodMap = new HashMap<>(16);
        if (CollectionUtils.isNotEmpty(goods)) {
            for (GoodsEntity single : goods) {
                goodMap.put(single.getBgId(), single);
            }
        }
        // 7、组装返回值信息。
        for (LiveGoodsStatsConsoleRspVO single : records) {
            single.setName(goodMap.containsKey(single.getBgId()) ? goodMap.get(single.getBgId()).getName() : "");
            single.setRoomId(roomInfo.getRoomId());
        }
        return new GoodsPage<>(reqVO.getPage(), reqVO.getPageSize(), (int) total, records, GoodsConstant.LIVE_GOODS_ENTRANCE_SWITCH_NO_SENSE);
    }

}
